<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>设备列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">设备</a>
        <a>
          <cite>设备列表</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input class="layui-input" placeholder="注册开始日" name="startTime" id="startTime">
          <input class="layui-input" placeholder="注册截止日" name="endTime" id="endTime">
          <input type="text" name="keywords"  placeholder="请输入关键字" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <span class="x-right" style="line-height:40px">共有数据：<span id="total"></span> 条</span>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>
              <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>ID</th>
            <th>主板ID</th>
            <th>设备名称</th>
            <th>注册时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </thead>
        <tbody id="data-body">
          <tr>
            <td>
              <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='2'><i class="layui-icon">&#xe605;</i></div>
            </td>
            <td>1</td>
            <td>admin</td>
            <td>18925139194</td>
            <td>超级管理员</td>
            <td>2017-01-01 11:11:42</td>
            <td class="td-manage">
              <a onclick="member_stop(this,'10001')" href="javascript:;"  title="启用">
                <i class="layui-icon">&#xe601;</i>
              </a>
              <a title="编辑"  onclick="x_admin_show('编辑','admin-edit.html')" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
              </a>
              <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="page">
        <div>
          <a class="prev" href="">&lt;&lt;</a>
          <a class="num" href="">1</a>
          <span class="current">2</span>
          <a class="num" href="">3</a>
          <a class="num" href="">489</a>
          <a class="next" href="">&gt;&gt;</a>
        </div>
      </div>

    </div>
    <script>
      layui.use(['laydate','form'], function(){
        let laydate = layui.laydate;
        let form = layui.form;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#startTime' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#endTime' //指定元素
        });

        //监听搜索提交
        form.on('submit(search)', function(data){
          getPageData(1,pageSize);
          return false;
        });
      });

       /*用户-停用*/
      function member_stop(obj,id){
          layer.confirm('确认要停用吗？',function(index){

              if($(obj).attr('title')=='启用'){

                //发异步把用户状态进行更改
                $(obj).attr('title','停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!',{icon: 5,time:1000});

              }else{
                $(obj).attr('title','启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!',{icon: 5,time:1000});
              }
              
          });
      }

      /*用户-删除*/
      function member_del(obj,id){
          layer.confirm('确认要删除吗？',function(index){
              //发异步删除数据
              $.ajax({
                type:"post",
                url:"/device/delete",
                data:{ids:[id]},
                dataType:"json",
                success:function(res){
                  if(res.code === 200){
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!',{icon:1,time:1000});
                    //重新查询数据
                    getPageData(nowPage,pageSize);
                  }
                }
              })


          });
      }



      function delAll (argument) {
        //需要删除的数据的id数组
        let data = tableCheck.getData();

        layer.confirm('确认要删除吗？',function(index){
            //捉到所有被选中的，发异步进行删除
            $.ajax({
              type:"post",
              url:"/device/delete",
              data:{ids:data},
              dataType:"json",
              success:function(res){
                if(res.code === 200){
                  layer.msg('删除成功', {icon: 1});
                  $(".layui-form-checked").not('.header').parents('tr').remove();
                  //重新查询数据
                  getPageData(nowPage,pageSize);
                }
              }
            })


        });
      }

      //##########################分页代码################################
      let nowPage = 1;
      let pageSize = 5;

      /*初始化页码*/
      function initPages(data) {
        //容器
        let div = $("<div></div>");
        //上一页
        let pre = $('<a class="prev" pre-page="'+data.prePage+'" has-prev-page="'+data.hasPreviousPage+'" href="javascript:prePage();">&lt;&lt;</a>');
        div.append(pre);
        //动态地渲染页数
        $(data.navigatepageNums).each(function(){
          let pageA;
          if(data.pageNum == this){
            pageA = $('<span class="current">'+this+'</span>');
          }else{
            pageA = $('<a class="num" href="javascript:changePage('+this+');">'+this+'</a>');
          }
          div.append(pageA);
        });
        //下一页
        let next = $('<a class="next" next-page="'+data.nextPage+'" has-next-page="'+data.hasNextPage+'" href="javascript:nextPage();">&gt;&gt;</a>');
        div.append(next);

        //加入外层容器
        $(".page").empty();
        $(".page").append(div);
      }

      /*上一页*/
      function prePage() {
        //获取上一页
        let prevPageNum = $(".prev").attr("pre-page");
        //是否有上一页
        let hasPrevPage = $(".prev").attr("has-prev-page");
        //调用获取数据的方法
        if(hasPrevPage === "true"){
          getPageData(prevPageNum,pageSize);
        }else{
          layer.msg("已经是第一页啦！");
        }
      }

      /*中间的页码跳转*/
      function changePage(page) {
        getPageData(page,pageSize);
      }

      /*下一页*/
      function nextPage() {
        //获取下一页
        let nextPageNum = $(".next").attr("next-page");
        //是否有下一页
        let hasNextPage = $(".next").attr("has-next-page");
        //调用获取数据的方法
        if(hasNextPage === "true"){
          getPageData(nextPageNum,pageSize);
        }else{
          layer.msg("已经是最后一页啦！");
        }
      }

      /*封装查询条件*/
      function getCondition() {
        let data = {};
        //获取开始时间
        let startTime = $("#startTime").val();
        if(startTime.length > 0)
          data.startTime = startTime+" 00:00:00";
        //获取结束时间
        let endTime = $("#endTime").val();
        if(endTime.length > 0)
          data.endTime = endTime+" 23:59:59";
        //获取关键字
        let keywords = $("input[name=keywords]").val();
        if(keywords.length > 0)
          data.keywords = keywords;
        return data;
      }
      
      /*查询数据的方法*/
      function getPageData(page,pageSize){
        //获取条件
        let condition = getCondition();
        //加上分页条件
        condition.page = page;
        condition.pageSize =  pageSize;
        console.log(condition);

        // 获取数据列表
        $.ajax({
          type:"post",
          url:"/device/list",
          // data:{page: page, pageSize: pageSize},
          data:condition,
          dataType:"json",
          success:function (data) {
            console.log(data);

            if(data.code === 200){
              //维护当前页的全局变量nowPage
              nowPage = data.data.pageNum;
              // 渲染总的条数
              $("#total").text(data.data.total);
              // 清空数据
              $("#data-body").empty();
              // 遍历数据的数组,生成对应的行
              $(data.data.list).each(function () {
                // 创建数据行的dom对象
                let tr = $('<tr>\n' +
                        '            <td>\n' +
                        '              <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="'+this.id+'"><i class="layui-icon">&#xe605;</i></div>\n' +
                        '            </td>\n' +
                        '            <td>'+this.id+'</td>\n' +
                        '            <td>'+this.boardId+'</td>\n' +
                        '            <td>'+this.computerName+'</td>\n' +
                        '            <td>'+this.createTime+'</td>\n' +
                        '            <td>'+(this.updateTime ? this.updateTime : "未更新")+'</td>\n' +
                        '            <td class="td-manage">\n' +
                        '              <a title="扫描类型列表-'+this.computerName+'" class="open_new_tab" _href="/scan/type/list/page?boardId='+this.boardId+'" href="javascript:;">\n' +
                        '                <i class="layui-icon">&#xe66b;</i>\n' +
                        '              </a>\n' +
                        '              <a title="删除" onclick="member_del(this,'+this.id+')" href="javascript:;">\n' +
                        '                <i class="layui-icon">&#xe640;</i>\n' +
                        '              </a>\n' +
                        '            </td>\n' +
                        '          </tr>');
                // 将数据追加到 tbody中
                $("#data-body").append(tr);

              });

              //初始化页码
              initPages(data.data);

            }

          }
        });
      }

      /**
       * 让整个dom文档加载完之后再执行
       */
      $(function () {
        getPageData(nowPage,pageSize);
      })
    </script>

  </body>

</html>